﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MaasOne;
using MaasOne.Base;
using MaasOne.Finance.YahooFinance;

namespace TestYahooFinanceManagedClient
{
    class TestApp01
    {
        public static TestApp01 Instance
        {
            get
            {
                return instance;
            }
        }

        private static TestApp01 instance = new TestApp01();

        private TestApp01()
        {
        }

        public void DownloadIdList()
        {
            AlphabeticIDIndexDownload dl = new AlphabeticIDIndexDownload();
            DownloadClient<AlphabeticIDIndexResult> baseDl = dl;
            AlphabeticIDIndexSettings settings = dl.Settings;
            //settings.TopIndex.Index = "A";
            SettingsBase baseSettings = baseDl.Settings;
            Response<AlphabeticIDIndexResult> resp = baseDl.Download();

            ConnectionInfo connInfo = resp.Connection;
            if (connInfo.State == ConnectionState.Success)
            {
                AlphabeticIDIndexResult result = resp.Result;
                AlphabeticalIndex[] list = result.Items;
                for (int i = 0; i < list.Length; ++i)
                {
                    Console.WriteLine(list[i].ToString());
                }
            }
            else
            {
                Exception ex = connInfo.Exception;
                Console.WriteLine(ex.Message);
            }

        }

        public void ExampleFromCodeProject01()
        {
            QuotesDownload dl = new QuotesDownload();
            DownloadClient<QuotesResult> baseDl = dl;

            QuotesDownloadSettings settings = dl.Settings;
            settings.IDs = new string[] { "MSFT", "GOOG", "YHOO" };
            settings.Properties = new QuoteProperty[] { QuoteProperty.Symbol,
                                            QuoteProperty.Name, 
                                            QuoteProperty.LastTradePriceOnly
                                          };
            SettingsBase baseSettings = baseDl.Settings;

            Response<QuotesResult> resp = baseDl.Download();

            ConnectionInfo connInfo = resp.Connection;
            if (connInfo.State == ConnectionState.Success)
            {
                QuotesResult result = resp.Result;
                //...
            }
            else
            {
                Exception ex = connInfo.Exception;
                Console.WriteLine(ex.Message);
            }
        }

        public void ExampleFromCodeProject02()
        {
            //Download TopIndex (e.g. [A], [B], [1-9])
            AlphabeticIDIndexDownload dl = new AlphabeticIDIndexDownload();
            dl.Settings.TopIndex = null;
            Response<AlphabeticIDIndexResult> resp = dl.Download();

            //Download Index (e.g. [C], [Cl], [Ca(1/10)])
            AlphabeticalTopIndex topIndex = (AlphabeticalTopIndex)resp.Result.Items[2];
            dl.Settings.TopIndex = topIndex;
            Response<AlphabeticIDIndexResult> resp2 = dl.Download();

            //Download ID Search Results
            AlphabeticalIndex index = resp.Result.Items[0];
            IDSearchDownload dl2 = new IDSearchDownload();
            Response<IDSearchResult> resp3 = dl2.Download(index);

            IDSearchData[] idResults = resp3.Result.Items;

            Console.WriteLine("Done");
        }
        /*
        public void ExampleFromStackOverflow01()
        {
            AlphabeticIDIndexDownload dl1 = new AlphabeticIDIndexDownload();
            dl1.Settings.TopIndex = null;
            Response<AlphabeticIDIndexResult> resp1 = dl1.Download();

            writeStream.WriteLine("Id|Isin|Name|Exchange|Type|Industry");

            foreach (var alphabeticalIndex in resp1.Result.Items)
            {
                AlphabeticalTopIndex topIndex = (AlphabeticalTopIndex)alphabeticalIndex;
                dl1.Settings.TopIndex = topIndex;
                Response<AlphabeticIDIndexResult> resp2 = dl1.Download();

                foreach (var index in resp2.Result.Items)
                {
                    IDSearchDownload dl2 = new IDSearchDownload();
                    Response<IDSearchResult> resp3 = dl2.Download(index);


                    int i = 0;
                    foreach (var item in resp3.Result.Items)
                    {
                        writeStream.WriteLine(item.ID + "|" + item.ISIN + "|" + item.Name + "|" + item.Exchange + "|" + item.Type + "|" + item.Industry);
                    }

                }
            }
        }
        */
    }
}
